<template>
	<view class="content">
		<!-- 搜索框 -->
		<view class="cu-bar bg-green search fixed" :style="[{top:CustomBar + 'px'}]">
			<view class="search-form round">
				<text class="cuIcon-search"></text>
				<input type="text" placeholder="请输入客户名称/合同编号/合同名称" confirm-type="search" @input="handleSearch"></input>
			</view>
		</view>
		<mescroll-uni top="100upx" class="my-mescroll" ref="mescrollRef" @init="mescrollInit" :down="downOption" :up="upOption" @down="downCallback" @up="upCallback">

		<view class="con-list">
			<view class="item" @click="showDetail(item)" v-for="(item, index) in dataList" >
				<view class="head">
					<text class="name">{{item.name}}</text>
					<view class="state-box">
						<!-- param info success -->
						<view :class="item.checkStatus==1?'state success':'state info'" >
							{{utils.getDictLabelName("contractOpStatus",item.status)}}
						</view>
					</view>
				</view>
				
				<view class="rate">
					<view class="left">
						<text class="text">回款进度</text>
						<view class="progress-box">
							<view class="cu-progress round">
								<view class="bg-green" :style="{ width: parseInt(item.receivedMoney/item.money*100)+'%' }"></view>
							</view>
							<text class="margin-left">{{parseInt(item.receivedMoney/item.money*100)}}%</text>
						</view>
					</view>
					<text class="money">￥{{item.money}}</text>
				</view>
				<view class="text">
					{{item.customerName}}
				</view>
				<view class="payment">
					已回款：￥{{item.receivedMoney}} 待回款：￥{{item.unreceivedMoney}}
				</view>
				<view class="payment">
					签约时间：{{item.orderDate}}
				</view>
			</view>
		</view>
		</mescroll-uni>
		
		<view class="pub-add-box" @click="addContract">
			<image class="img" src="../../../static/images/icon_add.png" mode="aspectFill"></image>
		</view>
	</view>
</template>

<script>
	import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins.js';
	import api from '@/api/api.js';
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				mescroll:undefined,
				downOption: {
					auto: false //是否在初始化后,自动执行downCallback; 默认true
				},
				upOption: {
					onScroll:true,
					use: true, // 是否启用上拉加载; 默认true
					auto: true, // 是否在初始化完毕之后自动执行上拉加载的回调; 默认true
					page: {
						num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 1, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						tip: '暂无相关数据'
					}
				},
				dataList: [],
				searchContent:'',
				progress: '80%', // 进度条
			}
		},
		onLoad() {
			// 修改顶部导航背景色
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: '#39b54a',
				animation: {
					duration: 400,
					timingFunc: 'easeIn'
				}
			})
		},
		onShow() {
			this.mescroll.resetUpScroll()
		},
		methods: {
			// mescroll组件初始化的回调,可获取到mescroll对象
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*下拉刷新的回调 */
			downCallback(mescroll) {
				mescroll.resetUpScroll()
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				this.getContractList(page);
			},
			getContractList(page){
				var data = {page:page.num,searchVal:this.searchContent};
				var that=this;
				api.getContractList(data).then(
					res => {
						if(res.code==200){
							console.log(res.data)
							console.log(page.num)
							//设置列表数据
							if (page.num == 1) {
								that.dataList = res.data.list; 
								
							} else {
								that.dataList = that.dataList.concat(res.data.list);
								 
							}
							that.mescroll.endBySize(res.data.list.length, res.data.total);
							
						}else{
							uni.showToast({
								icon:'none',
								title: "请求失败",
							});
							that.dataList = null;
							mescroll.endErr();
							// that.mescroll.endSuccess(0);
						}
					},
					rej => {}
				);
			},
			// 查看客户详情
			showCusDetail(item){
				uni.navigateTo({
					url: './customDetail?customerId='+item.customerId
				})
			},
			// 新建合同
			addContract(){
				uni.navigateTo({
					url: './addContract'
				})
			},
			// 顶部搜索
			handleSearch(e) {
				let key = e.detail.value
				console.log(key)
				this.searchContent=key;
				this.upCallback({num:1,size:10})
			},
			// 查看产品详情
			showDetail(item){
				uni.navigateTo({
					url: './detail?contractId=' + item.contractId
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.cu-progress{
		height: 20upx;
	}
	.content{
		.con-list{
			background: #fff;
			.item{
				padding: 20upx;
				border-bottom: 1px solid #f7f7f7;
				&:last-child{
					border-bottom: none;
				}
				.head{
					display: flex;
					align-items: center;
					justify-content: space-between;
					.name{
						font-size: 32upx;
						color: #000;
					}
					.state-box{
						.state{
							width: 120upx;
							padding: 10upx 0;
							font-size: 24upx;
							text-align: center;
							&.param{
								background: #EBF3FE;
								color: #458BEB;
							}
							&.info{
								background: #F8F8F8;
								color: #BFBFBF;
							}
							&.success{
								background: #E7F7ED;
								color: #2FAC5F;
							}
						}
					}
				}
				.rate{
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding: 10upx 0;
					.left{
						width: 90%;
						display: flex;
						align-items: center;
						.text{
							font-size: 30upx;
							color: #999;
						}
						.progress-box{
							align-items: center;
							display: flex;
							width: 60%;
							margin-left: 20upx;
						}
					}
					.money{
						font-size: 32upx;
						color: #E6A23C;
						font-weight: bold;
					}
				}
				
				.payment{
					font-size: 28upx;
					color: #999;
				}
			}
		}
	}
	.pub-add-box{
			z-index: 9999;
			width: 120upx;
			height: 120upx;
			border-radius: 50%;
			background: #0aAC44;
			position: fixed;
			right: 60upx;
			bottom: 60upx;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			box-shadow:0px 0px 15upx #0AAC44;
			.img{
				width: 40upx;
				height: 40upx;
			}
		}
</style>
